In [1]:
# Auto-generated by leda
import leda
leda.set_interact_mode(leda.StaticIpywidgetsInteractMode())
In [2]:
# Auto-generated by leda
import os
from leda.vendor.static_ipywidgets.static_ipywidgets import interact as static_interact
static_interact.IMAGE_MANAGER = static_interact.InlineImageManager()
leda demo: matplotlib¶
In [3]:
import abc
import dataclasses
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from typing_extensions import override
import leda
In [4]:
leda.init("matplotlib")
plt.rcParams.update({"figure.max_open_warning": 0})
In [5]:
%%HTML
<!-- Auto-generated by leda -->
<h2>Table of Contents</h2>
<ol type='I'>
<li type='I'><a href='#leda-demo:-matplotlib'>leda demo: matplotlib</a></li>
<ol type='A'>
<li type='A'><a href='#Info'>Info</a></li>
<li type='A'><a href='#Data'>Data</a></li>
<li type='A'><a href='#Visualization'>Visualization</a></li>
<ol type='1'>
<li type='1'><a href='#Simple'>Simple</a></li>
<li type='1'><a href='#Objects-as-Params'>Objects as Params</a></li>
</ol>
</ol>
</ol>
Table of Contents
Info¶
Widgets
Use the %%interact expr0;expr1;... cell magic to set widgets for that cell.
Each expression is of the form x=y, where x becomes the local var of the cell and y can be a:
listto indicate choices for a dropdown widgettupleto indicate values for an int slider (start, stop, and optional step).
E.g.:
%%interact column=list(df.columns)
%%interact column=list(df.columns);mult=[1, 2, 3]
%%interact column=list(df.columns);window=(10, 50)
%%interact column=list(df.columns);window=(10, 50, 5)
Table of Contents
Use the %toc line magic to substitute with a table of contents in static mode.
Toggles
Click the Toggle input cells button at the bottom to reveal input cells.
Data¶
Using randomly generated data (with fixed seed).
In [6]:
df = pd.DataFrame(
np.random.RandomState(42).rand(100, 10), columns=list("abcdefghij")
)
Visualization¶
Simple¶
In [7]:
%%interact column=list(df.columns);mult=[1, 2, 3]
title = f"column={column!r}, mult={mult}"
(df[[column]] * mult).plot(figsize=(15, 8), lw=2, title=title)
Generating results: 0%| | 0/30 [00:00<?, ?it/s]
Generating results: 53%|█████████████████████████████████████████████████████████████████████████████████▌ | 16/30 [00:00<00:00, 159.48it/s]
Generating results: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:00<00:00, 128.27it/s]
Generating HTML: 0%| | 0/30 [00:00<?, ?it/s]
Generating HTML: 7%|██████████▌ | 2/30 [00:00<00:01, 18.21it/s]
Generating HTML: 13%|█████████████████████ | 4/30 [00:00<00:01, 19.14it/s]
Generating HTML: 20%|███████████████████████████████▌ | 6/30 [00:00<00:01, 18.97it/s]
Generating HTML: 27%|██████████████████████████████████████████▏ | 8/30 [00:00<00:01, 19.23it/s]
Generating HTML: 37%|█████████████████████████████████████████████████████████▌ | 11/30 [00:00<00:00, 19.54it/s]
Generating HTML: 47%|█████████████████████████████████████████████████████████████████████████▎ | 14/30 [00:00<00:00, 19.80it/s]
Generating HTML: 57%|████████████████████████████████████████████████████████████████████████████████████████▉ | 17/30 [00:00<00:00, 19.95it/s]
Generating HTML: 67%|████████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 20/30 [00:01<00:00, 20.11it/s]
Generating HTML: 77%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 23/30 [00:01<00:00, 20.19it/s]
Generating HTML: 87%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 26/30 [00:01<00:00, 20.18it/s]
Generating HTML: 97%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 29/30 [00:01<00:00, 20.12it/s]
Generating HTML: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:01<00:00, 19.87it/s]
Out[7]:
mult:
In [8]:
%%interact column=list(df.columns);window=(10, 50, 5)
ax = (
df[[column]]
.iloc[-window:]
.plot(figsize=(15, 8), lw=2, title=f"column={column!r}, window={window}")
)
ax
Generating results: 0%| | 0/90 [00:00<?, ?it/s]
Generating results: 16%|███████████████████████▉ | 14/90 [00:00<00:00, 99.98it/s]
Generating results: 34%|████████████████████████████████████████████████████▋ | 31/90 [00:00<00:00, 134.20it/s]
Generating results: 53%|█████████████████████████████████████████████████████████████████████████████████▌ | 48/90 [00:00<00:00, 125.66it/s]
Generating results: 73%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 66/90 [00:00<00:00, 141.09it/s]
Generating results: 93%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 84/90 [00:00<00:00, 151.29it/s]
Generating results: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:00<00:00, 131.09it/s]
Generating HTML: 0%| | 0/90 [00:00<?, ?it/s]
Generating HTML: 3%|█████▎ | 3/90 [00:00<00:04, 20.82it/s]
Generating HTML: 7%|██████████▌ | 6/90 [00:00<00:03, 21.52it/s]
Generating HTML: 10%|███████████████▊ | 9/90 [00:00<00:03, 21.68it/s]
Generating HTML: 13%|████████████████████▉ | 12/90 [00:00<00:03, 22.10it/s]
Generating HTML: 17%|██████████████████████████▏ | 15/90 [00:00<00:03, 22.25it/s]
Generating HTML: 20%|███████████████████████████████▍ | 18/90 [00:00<00:03, 21.61it/s]
Generating HTML: 23%|████████████████████████████████████▋ | 21/90 [00:00<00:03, 21.77it/s]
Generating HTML: 27%|█████████████████████████████████████████▊ | 24/90 [00:01<00:03, 21.85it/s]
Generating HTML: 30%|███████████████████████████████████████████████ | 27/90 [00:01<00:02, 21.89it/s]
Generating HTML: 33%|████████████████████████████████████████████████████▎ | 30/90 [00:01<00:02, 22.32it/s]
Generating HTML: 37%|█████████████████████████████████████████████████████████▌ | 33/90 [00:01<00:02, 22.17it/s]
Generating HTML: 40%|██████████████████████████████████████████████████████████████▊ | 36/90 [00:01<00:02, 22.12it/s]
Generating HTML: 43%|████████████████████████████████████████████████████████████████████ | 39/90 [00:01<00:02, 21.78it/s]
Generating HTML: 47%|█████████████████████████████████████████████████████████████████████████▎ | 42/90 [00:01<00:02, 21.84it/s]
Generating HTML: 50%|██████████████████████████████████████████████████████████████████████████████▌ | 45/90 [00:02<00:02, 21.98it/s]
Generating HTML: 53%|███████████████████████████████████████████████████████████████████████████████████▋ | 48/90 [00:02<00:01, 22.22it/s]
Generating HTML: 57%|████████████████████████████████████████████████████████████████████████████████████████▉ | 51/90 [00:02<00:01, 22.25it/s]
Generating HTML: 60%|██████████████████████████████████████████████████████████████████████████████████████████████▏ | 54/90 [00:02<00:01, 22.07it/s]
Generating HTML: 63%|███████████████████████████████████████████████████████████████████████████████████████████████████▍ | 57/90 [00:02<00:01, 22.07it/s]
Generating HTML: 67%|████████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 60/90 [00:02<00:01, 22.01it/s]
Generating HTML: 70%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████▉ | 63/90 [00:02<00:01, 21.77it/s]
Generating HTML: 73%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 66/90 [00:03<00:01, 21.98it/s]
Generating HTML: 77%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 69/90 [00:03<00:00, 21.86it/s]
Generating HTML: 80%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 72/90 [00:03<00:00, 21.72it/s]
Generating HTML: 83%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 75/90 [00:03<00:00, 22.08it/s]
Generating HTML: 87%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 78/90 [00:03<00:00, 22.07it/s]
Generating HTML: 90%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 81/90 [00:03<00:00, 22.00it/s]
Generating HTML: 93%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 84/90 [00:03<00:00, 22.28it/s]
Generating HTML: 97%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 87/90 [00:03<00:00, 22.55it/s]
Generating HTML: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:04<00:00, 22.61it/s]
Generating HTML: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:04<00:00, 22.06it/s]
Out[8]:
window:
Objects as Params¶
In [9]:
@dataclasses.dataclass(frozen=True)
class Calculator(abc.ABC):
@abc.abstractmethod
def calc(self, df: pd.DataFrame) -> pd.DataFrame: ...
@dataclasses.dataclass(frozen=True)
class CumSumCalculator(Calculator):
@override
def calc(self, df: pd.DataFrame) -> pd.DataFrame:
return df.cumsum()
@dataclasses.dataclass(frozen=True)
class EWMMeanCalculator(Calculator):
com: float
@override
def calc(self, df: pd.DataFrame) -> pd.DataFrame:
return df.ewm(com=self.com).mean()
calcs = [
CumSumCalculator(),
EWMMeanCalculator(com=5),
EWMMeanCalculator(com=10),
]
In [10]:
%%interact column_group=["abc", "def", "ghij"];calc=calcs
calced_df = calc.calc(df[list(column_group)])
title = f"column_group={column_group!r}, calc={calc}"
calced_df.plot(figsize=(15, 8), lw=2, title=title)
Generating results: 0%| | 0/9 [00:00<?, ?it/s]
Generating results: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 149.31it/s]
Generating HTML: 0%| | 0/9 [00:00<?, ?it/s]
Generating HTML: 33%|█████████████████████████████████████████████████████ | 3/9 [00:00<00:00, 21.04it/s]
Generating HTML: 67%|██████████████████████████████████████████████████████████████████████████████████████████████████████████ | 6/9 [00:00<00:00, 19.36it/s]
Generating HTML: 89%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 8/9 [00:00<00:00, 19.20it/s]
Generating HTML: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 19.14it/s]
Out[10]:
column_group:
In [11]:
# Auto-generated by leda
import leda
leda.show_input_toggle()
Out[11]:
In [12]:
# Auto-generated by leda
import leda
leda.show_std_output_toggle()
Out[12]: